package com.situ.company.department.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.util.JDBCUtil;

public class DepartmentDao {

	private static String table = "department";
	private static String cols = "code,name,tel";
	private Connection conn;
	private PreparedStatement ps;

	// public Integer insert(DepartmentModel model){
	// StringBuffer sql = new StringBuffer("insert into ");
	// sql.append(table).append('(').append(cols).append(")values(?,?,?)");
	// try {
	// conn = JDBCUtil.getConnertion();
	// ps = conn.prepareStatement(sql.toString());
	// ps.setString(1, model.getCode());
	// ps.setString(2, model.getName());
	// ps.setString(3, model.getTel());
	// return ps.executeUpdate();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }finally{
	// JDBCUtil.close(conn, ps);
	// }
	// return null;
	// }
	/**
	 * 添加
	 * 
	 * @param model
	 * @return
	 */
	public Integer insert(DepartmentModel model) {
		String sql = "insert into department(code,name,tel)values(?,?,?)";
		// StringBuffer sql = new StringBuffer("insert into ");
		// sql.append(table).append('(').append(cols).append(")values(?,?,?)");
		return JDBCUtil.update(sql.toString(), model.getCode(), model.getName(), model.getTel());
	}

	/**
	 * 查找
	 * 
	 * @param model
	 * @return
	 */
	public List<DepartmentModel> selectList(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("select ");
		sql.append(cols).append(",(select count(id) from employee where code_dept=department.code) count ")
				.append(" from ").append(table);
		List<Object> vals = appendWhere(sql, model);
		ResultSet rs = null;
		List<DepartmentModel> list = new ArrayList<>();
		try {
			conn = JDBCUtil.getConnertion();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++)
				ps.setObject(i + 1, vals.get(i));
			rs = ps.executeQuery();
			while (rs.next()) {
				DepartmentModel mdb = new DepartmentModel();
				mdb.setCode(rs.getString("code"));
				mdb.setName(rs.getString("name"));
				mdb.setTel(rs.getString("tel"));
				mdb.setCount(rs.getInt("count"));
				list.add(mdb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	public List<DepartmentModel> selectList1(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("select ");
		sql.append(cols).append(",(select count(id) from employee where code_dept=department.code) count1 ")
				.append(" from ").append(table);
		List<Object> vals = appendWhere(sql, model);
		Map<String, String> map = new HashMap<>();
		map.put("code", "code");
		map.put("name", "name");
		map.put("tel", "tel");
		map.put("count", "count1");
		return JDBCUtil.query(sql.toString(), vals, DepartmentModel.class, map);
	}

	// public Integer delete(DepartmentModel model){
	// StringBuffer sql = new StringBuffer("delete from ");
	// sql.append(table);//where code = ?
	// List<Object> vals = appendWhere(sql, model);
	// try {
	// conn = JDBCUtil.getConnertion();
	// ps = conn.prepareStatement(sql.toString());
	// for(int i = 0; i < vals.size(); i++)
	// ps.setObject(i + 1, vals.get(i));
	// return ps.executeUpdate();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }finally{
	// JDBCUtil.close(conn, ps);
	// }
	// return null;
	// }

	/**
	 * 删除
	 * 
	 * @param model
	 * @return
	 */
	public Integer delete(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("delete from ");
		sql.append(table);
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	// public Integer update(DepartmentModel model){
	// StringBuffer sql = new StringBuffer("update ");
	// sql.append(table);//set name=?,tel=? where code=?
	// List<Object> vals = appendSet(sql, model);
	// try {
	// conn = JDBCUtil.getConnertion();
	// ps = conn.prepareStatement(sql.toString());
	// for(int i = 0; i < vals.size(); i++)
	// ps.setObject(i + 1, vals.get(i));
	// return ps.executeUpdate();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }finally{
	// JDBCUtil.close(conn, ps);
	// }
	// return null;
	// }
	/**
	 * 修改
	 * 
	 * @param model
	 * @return
	 */
	public Integer update(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("update ");
		sql.append(table);
		List<Object> vals = appendSet(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	/**
	 * 拼接条件
	 * 
	 * @param sql
	 * @param model
	 * @return
	 */
	private List<Object> appendWhere(StringBuffer sql, DepartmentModel model) {
		sql.append(" where 1=1 ");
		List<Object> objs = new ArrayList<>();
		String code = model.getCode();
		// if(code != null && !"".equals(code.trim())){
		//
		// }
		if (code != null && !code.trim().isEmpty()) {// !"".equals(code.trim())
			sql.append(" and code like ? ");
			objs.add(code);
		}
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) {// !"".equals(code.trim())
			sql.append(" and name like ? ");
			objs.add(name);
		}
		String tel = model.getTel();
		if (tel != null && !tel.trim().isEmpty()) {// !"".equals(code.trim())
			sql.append(" and tel like ? ");
			objs.add(tel);
		}
		String order = model.getOrderby();
		if (order != null && !order.trim().isEmpty()) {
			sql.append(" order by ").append(order);
		}
		if (model.isPageOn()) {
			sql.append(" limit ?,? ");
			objs.add(model.getRowStart());
			objs.add(model.getRowCount());
		}
		return objs;
	}

	/**
	 * 修改拼接 不为空在后面追加
	 * 
	 * @param sql
	 * @param model
	 * @return
	 */
	private List<Object> appendSet(StringBuffer sql, DepartmentModel model) {
		sql.append(" set id=id ");
		List<Object> objs = new ArrayList<>();
		String name = model.getName();
		if (name != null) {// !"".equals(code.trim())
			sql.append(" ,name = ? ");
			objs.add(name);
		}
		String tel = model.getTel();
		if (tel != null) {// !"".equals(code.trim())
			sql.append(" ,tel = ? ");
			objs.add(tel);
		}
		sql.append(" where code = ?");
		objs.add(model.getCode());
		return objs;
	}

	/**
	 * 记录条数
	 * 
	 * @param model
	 * @return
	 */
	public int selectCount(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("select count(id) from ");
		sql.append(table);
		List<Object> vals = appendWhere(sql, model);
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnertion();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++)
				ps.setObject(i + 1, vals.get(i));
			rs = ps.executeQuery();
			if (rs.next())
				return rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return 0;
	}
}
